//设置请求头前缀BasicUrl
// const BasicUrl = "http://192.168.2.253:8080" //本地测试环境
// const BasicUrl = "https://gasmate.backend.jinmi.lengshan.com.cn" //正式环境
const BasicUrl = "https://backend.gasmate.jinmi.lengshan.com.cn" //正式环境
// const BasicUrl = "" //正式环境

//不需要token的数据请求
const Request = (url, data = {}, method) => {
	// 传入请求参数，返回一个promise对象,参数是一个携带resolve，reject回调函数的的立即执行函数
	return new Promise((resolve, reject) => {
		// 这里执行数据请求异步操作
		uni.request({
			url: BasicUrl + url,
			method,
			data: data,
			header: {
				'content-type': method == 'DELETE' || method == 'PUT' ? 'application/x-www-form-urlencoded;charset=UTF-8' : 'application/json;charset=utf-8'
			},
			success: (res) => {
				// 成功回调：执行resolve
				resolve(res) //把promise对象的状态置为已完成,携带返回结果res
			},
			fail: (err) => {
				// 失败回调：执行reject
				reject(err) //把promise对象的状态置为失败,携带错误原因err
			}
		})
	})
}


// 需要携带token的请求
const RequestToken = (url, data = {}, method) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: method == 'DELETE' ? BasicUrl + url + '/' +  data.userIds : BasicUrl + url,
			method,
			data: data,
			header: {
				'content-type': method == 'DELETE' || (method == 'PUT' && url !== '/core/price') && (url !== '/system/user') ? 'application/x-www-form-urlencoded;charset=UTF-8' : 'application/json;charset=utf-8',
				'Authorization': uni.getStorageSync('token')
			},
			success: (res) => {
				resolve(res)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}
// 请求封装,实现自行判断是否携带token
export const request = (url, data = {}, method = 'GET', isToken = true) => {
	uni.showLoading({
		title: '',
	})
	return new Promise((resolve, reject) => {
		//  需要携带token的请求
		if (isToken) {
			let token = uni.getStorageSync('token')
			if (token) {
				// 缓存中存在token,发起RequestToken数据请求
				RequestToken(url, data, method).then(res => {
					if (res.data.code === 200) {
						resolve(res.data) //把promise对象的状态置为已完成,携带RequestToken数据请求成功结果res
						// uni.hideLoading()
					} else {
						if(res.data.code === 401) {
							uni.showToast({
								title: '登录过期，请重新登录',
								icon: 'none'
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/login/login'
								})
							},1500)
							
						}else {
							reject(res.data.msg)
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					}
				})
			} else {
				// 缓存中token为空，提示授权登录
				uni.showToast({
					title: '请重新登录',
					icon: 'error'
				})
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				},1500)
			}
		} else {
			//  不需要token的请求，发起Request
			Request(url, data, method).then(res => {
				if (res.data.code === 200) {
					resolve(res.data) //把promise对象的状态置为已完成,携带Request请求成功结果res
					// uni.hideLoading()
				} else {
					// uni.hideLoading()
					if(res.data.code === 401) {
						uni.showToast({
							title: '登录过期，请重新登录',
							icon: 'none'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						},1500)
					}else {
						reject(res.data.msg)
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}
					
				}
			})
		}

		uni.hideLoading()
	})
}